package components.criteria;

import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.Timer;

import menus.common.AbstractMenu;
import menus.common.statemachines.IStateMachine;
import menus.flower.FlowerMenuStateMachine;

import components.SetupExperiment;
import components.intertitles.Trial;
import components.intertitles.TrialInstructions;

import fr.inria.insitu.noe.Platform;
import fr.inria.insitu.noe.endConditions.AbstractEndCondition;
import fr.lri.swingstates.canvas.Canvas;
import fr.lri.swingstates.debug.StateMachineEvent;
import fr.lri.swingstates.sm.StateMachineListener;

/**
* WARNING: This criterion only works for Java AWT input system.
*
* @touchstone.criterion SelectionEnded
*/

public class SelectionEnded extends AbstractEndCondition {

	protected Canvas canvas;
	
	public SelectionEnded() {
		
	}
	
	@Override
	public boolean isReached(Timer timer, long when) {
        return false;
    }
	
	@Override
    public boolean isReached(InputEvent e) {
		
		if (e.getID() == MouseEvent.MOUSE_RELEASED 
				 && ((AbstractMenu)Platform.getInstance().getFactorValue(Trial.CurrentMenu)).isSelectionEnded()){
			
			System.out.println("SelectionEnded.isReached()");
			((IStateMachine)((AbstractMenu)Platform.getInstance().getFactorValue(Trial.CurrentMenu)).getStateMachine()).endSelection();
			
		}
		
		return (e.getID() == MouseEvent.MOUSE_RELEASED 
				 && ((AbstractMenu)Platform.getInstance().getFactorValue(Trial.CurrentMenu)).isSelectionEnded());

    }
	
	public void start() {
		
		canvas = ((SetupExperiment)Platform.getExperiment().getSetUp()).getGestureArea();
		
		super.start();
		Platform.getInstance().registerComponent(canvas);
		
	}

	public void stop() {
		super.stop();
	}
	
}
